package com.vietnam.power.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.vietnam.power.dao.ApplicationDao;
import com.vietnam.power.domain.AppConfiguration;
import com.vietnam.power.domain.PaymentMethod;
import com.vietnam.power.domain.PaymentMethodSearchCriteria;
import com.vietnam.power.service.AppConfigurationService;

@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
public class AppConfigurationServiceImpl implements AppConfigurationService {

	@Autowired
	private ApplicationDao applicationDao;

	public List<AppConfiguration> getAppConfigurations() {
		List<AppConfiguration> lst = new ArrayList<AppConfiguration>();
		for (com.vietnam.power.persitence.AppConfiguration a : applicationDao.getAppConfigurations()) {
			AppConfiguration ap = new AppConfiguration();
			ap.convertToDomain(a);
			lst.add(ap);
		}
		return lst;
	}

	public AppConfiguration getAppConfigurationByName(String name) {
		AppConfiguration ap = new AppConfiguration();
		ap.convertToDomain(applicationDao.getAppConfigurationByName(name));
		return ap;
	}

	public List<PaymentMethod> getPaymentMethod() {
		List<PaymentMethod> lst = new ArrayList<PaymentMethod>();

		for (com.vietnam.power.persitence.PaymentMethod pm : applicationDao.getPaymentMethod()) {
			PaymentMethod pmd = new PaymentMethod();
			pmd.convertToDomain(pm);
			lst.add(pmd);
		}

		return lst;
	}

	public PaymentMethod getPaymentMethodById(int id) {
		PaymentMethod pmd = new PaymentMethod();
		pmd.convertToDomain(applicationDao.getPaymentMethodById(id));
		return pmd;
	}

	public void createPaymentMethod(PaymentMethod pm) {
		com.vietnam.power.persitence.PaymentMethod ppm = new com.vietnam.power.persitence.PaymentMethod();
		pm.convertToPersist(ppm);
		
		applicationDao.createPaymentMethod(ppm);
		//update id
		pm.setId(ppm.getId());
	}

	public void updatePaymentMethod(PaymentMethod pm) {
		com.vietnam.power.persitence.PaymentMethod ppm = applicationDao.getPaymentMethodById(pm.getId());
		pm.convertToPersist(ppm);
		
		applicationDao.createPaymentMethod(ppm);
	}

	public void deletePaymentMethod(int id) {
		com.vietnam.power.persitence.PaymentMethod ppm = applicationDao.getPaymentMethodById(id);
		
		applicationDao.deletePaymentMethod(ppm);
	}

	public List<PaymentMethod> searchPaymentMethod(PaymentMethodSearchCriteria s) {
		List<PaymentMethod> lst = new ArrayList<PaymentMethod>();

		for (com.vietnam.power.persitence.PaymentMethod pm : applicationDao.searchPaymentMethod(s)) {
			PaymentMethod pmd = new PaymentMethod();
			pmd.convertToDomain(pm);
			lst.add(pmd);
		}

		return lst;
	}
}
